package com.jsmile.cloud.accountcenter.shop.service;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.accountcenter.api.enums.AccountCodeEnum;
import com.jsmile.cloud.accountcenter.api.exception.AccountException;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantCreate;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantQuery;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqTenantUpdate;
import com.jsmile.cloud.accountcenter.api.res.ResTenant;
import com.jsmile.cloud.accountcenter.shop.model.Tenant;
import com.jsmile.cloud.accountcenter.shop.repo.TenantRepository;
import com.jsmile.cloud.accountcenter.shop.repo.TenantSearchRepository;
import com.jsmile.cloud.authc.api.feign.UserServiceApi;
import com.jsmile.cloud.authc.api.req.ReqSysUserRegister;
import com.jsmile.cloud.authc.api.res.ResSysUser;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.enums.JSmileCacheEnum;
import com.jsmile.mall.cache.kit.SpringKit;

import cn.hutool.core.bean.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
@AllArgsConstructor
public class TenantApplicationService {

    private final TenantRepository tenantRepository;
    private final TenantSearchRepository tenantSearchRepository;

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public ResTenant addTenant(ReqTenantCreate reqTenantSave) {
        log.info("新增租户:{]", reqTenantSave);
        Tenant tenant = Tenant.create(reqTenantSave);
        tenantRepository.save(tenant);
        ReqSysUserRegister sysUserRegisterTo = tenant.registerUser();
        JSmileResult<ResSysUser> result = SpringKit.getBean(UserServiceApi.class).register(sysUserRegisterTo);// 注册商户用户
        if (!result.getResSuccess()) {
            throw new AccountException(result.getResCode(), result.getResMsg(), null);
        }
        return BeanUtil.toBean(tenant, ResTenant.class);
    }

    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'tenant_'+#p0.tenantId")
    public ResTenant updateTenant(ReqTenantUpdate reqTenantUpdate) {
        log.info("更新租户:{]", reqTenantUpdate);
        Tenant tenant = tenantSearchRepository.getById(reqTenantUpdate.getMerchantId());
        if (null == tenant) {
            throw new AccountException(AccountCodeEnum.TENANT_NOT_EXISTS);
        }
        tenant.updateTenant(reqTenantUpdate);
        tenantRepository.updateById(tenant);
        return BeanUtil.toBean(tenant, ResTenant.class);
    }

    @Cacheable(value = JSmileCacheEnum.JSmileCacheName.oneMonth, key = "'tenant_'+#tenantId")
    public ResTenant getTenant(long tenantId) {
        log.info("获取租户:{]", tenantId);
        Tenant tenant = tenantSearchRepository.getById(tenantId);
        if (null == tenant) {
            throw new AccountException(AccountCodeEnum.TENANT_NOT_EXISTS);
        }
        return BeanUtil.toBean(tenant, ResTenant.class);
    }

    public IPage<ResTenant> getTenantPageList(ReqTenantQuery reqTenantQuery) {
        log.info("租户查询:{]", reqTenantQuery);
        return tenantSearchRepository.pageQuery(reqTenantQuery).convert(x -> {
            return BeanUtil.toBean(x, ResTenant.class);
        });
    }

}
